package com.localjoycommunity.cpdj.project.system.service.impl;

/**
 * @author: aqie
 * @create: 2020-11-29 23:18
 **/

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.localjoycommunity.cpdj.project.system.domain.UserActivity;
import com.localjoycommunity.cpdj.project.system.domain.vo.UserPartyActivityHistoryVo;
import com.localjoycommunity.cpdj.project.system.domain.vo.UserPartyActivityListVo;
import com.localjoycommunity.cpdj.project.system.domain.vo.UserWishActivityListVo;
import com.localjoycommunity.cpdj.project.system.mapper.ActivityWishMapper;
import com.localjoycommunity.cpdj.project.system.mapper.UserActivityMapper;
import com.localjoycommunity.cpdj.project.system.mapper.UserWishMapper;
import com.localjoycommunity.cpdj.project.system.service.IUserActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * 用户活动Service业务层处理
 *
 * @author leepandar
 * @date 2020-11-16
 */
@Service
public class UserActivityServiceImpl extends ServiceImpl<UserActivityMapper, UserActivity> implements IUserActivityService {
    @Autowired
    private UserActivityMapper userActivityMapper;
    @Autowired
    private ActivityWishMapper activityWishMapper;
    @Autowired
    private UserWishMapper userWishMapper;

    /**
     * todo 查询用户活动列表
     *
     * @param userId 用户id
     * @return 用户活动
     */
    @Override
    public List<UserPartyActivityListVo> userPartyActivityList(Long userId, int stateCode) {
        return userActivityMapper.userPartyActivityList(userId, stateCode);
    }

    @Override
    public List<UserWishActivityListVo> userWishActivityList(Long userId, int stateCode) {
        return userWishMapper.userWishActivityList(userId, stateCode);
    }

    @Override
    public List<UserPartyActivityHistoryVo> userPartyActivityHistoryList() {
        return userActivityMapper.userPartyActivityHistoryList();
    }

    /**
     * 党建活动报名 / 取消
     */
    @Override
    @Transactional
    public int applyPartyActivity(UserActivity userActivity) {
        // 1. 查询是否存在
        UserActivity one = baseMapper.selectOne(new QueryWrapper<UserActivity>().lambda().eq(UserActivity::getUserId, userActivity.getUserId())
                .eq(UserActivity::getActivityId, userActivity.getActivityId()));
        if (one == null) {
            // 不存在 用户报名活动
            // 1. 活动剩余人数 -1
            activityWishMapper.updateRemainNum(userActivity.getActivityId(), -1);
            // 2. userActivity 表插入一条数据
            userActivity.setCreateTime(new Date());
            userActivity.setUpdateTime(new Date());
            userActivity.setStateCode(1);
            return baseMapper.insert(userActivity);
            // 3. 统计用户活动数???
        } else {
            // 更新 用户取消认领心愿 这里必须判断 stateCode
            //  用户取消 则活动剩余人数 + 1
            if(userActivity.getStateCode() == -1){
                activityWishMapper.updateRemainNum(userActivity.getActivityId(), 1);
            }else {
                activityWishMapper.updateRemainNum(userActivity.getActivityId(), -1);
            }

            userActivity.setId(one.getId());
            userActivity.setUpdateTime(new Date());
            userActivity.setStateCode(userActivity.getStateCode());
            return baseMapper.updateById(userActivity);
        }
    }

    /**
     * 查询用户 党建活动报名状态
     */
    @Override
    public int isApplyPartyActivity(Long userId, Long activityId) {
        return userActivityMapper.isApplyPartyActivity(userId, activityId).orElse(0);
    }

    /**
     * 新增用户活动
     *
     * @param userActivity 用户活动
     * @return 结果
     */
    @Override
    public int insertUserActivity(UserActivity userActivity) {
        return userActivityMapper.insertUserActivity(userActivity);
    }

    /**
     * 修改用户活动
     *
     * @param userActivity 用户活动
     * @return 结果
     */
    @Override
    public int updateUserActivity(UserActivity userActivity) {
        return userActivityMapper.updateUserActivity(userActivity);
    }

    /**
     * 批量删除用户活动
     *
     * @param ids 需要删除的用户活动ID
     * @return 结果
     */
    @Override
    public int deleteUserActivityByIds(Long[] ids) {
        return userActivityMapper.deleteUserActivityByIds(ids);
    }

    /**
     * 删除用户活动信息
     *
     * @param id 用户活动ID
     * @return 结果
     */
    @Override
    public int deleteUserActivityById(Long id) {
        return userActivityMapper.deleteUserActivityById(id);
    }
}

